<?php

/**
 * 解析htmlselect的选项单元
 * @param string $str 格式：value:text\n
 * @since 1.0 <2015-10-20> SoChishun Added.
 */
function parse_htmlselect_options($str, $value = '') {
    $out = '';
    $list = explode("\n", $str);
    foreach ($list as $row) {
        $arow = explode(':', $row, 2);
        if (count($arow) > 1) {
            $out.='<option value="' . $arow[0] . '"' . ($value == $arow[0] ? 'selected="selected"' : '') . '>' . $arow[1] . '</option>';
        }
    }
    return $out;
}

function afn_write_log($path, $flag) {
    if (!$path) {
        return;
    }
    if ('ALL' == $flag) {
        $flag = false;
    }
    $systypes = array('EMERG', 'ALERT', 'CRIT', 'ERR', 'WARN', 'NOTIC', 'INFO', 'DEBUG', 'SQL');
    $file = fopen(sofn_path_rtoa($path), 'r');
    $i = 1;
    $n = 0;
    echo '<div class="content">';
    echo '<div class="section">'; // section
    while (!feof($file)) {
        $sline = trim(fgetss($file, 2048));
        if (!$sline) {
            continue;
        }
        $n++;
        if ('[' == $sline[0]) {
            if ($n > 1) {
                echo '</div><div class="section">';
            }
            echo '<div class="title">', $sline, '</div>';
        } else {
            $type = substr($sline, 0, strpos($sline, ':'));
            if ($flag) {
                if ('OTHER' == $flag && in_array($type, $systypes)) {
                    continue;
                } else if ('OTHER' != $flag && $flag != $type) {
                    continue;
                }
            }
            echo '<div class="log">', $i, '. <a href="?module=', $module, '&logfile=', $logfile, '&flag=', $type, '">', $type, '</a>', substr($sline, strlen($type)), '</div>';
            $i++;
        }
    }
    fclose($file);
    echo '</div>'; // /section
    echo '</div>';
    echo '<div class="sum">共 ', $i, ' 条日志</div>';
}
/**
 * 翻译
 * @param type $val
 * @param type $type
 * @return string
 * @since 1.0 2016-2-1 SoChishun Added.
 */
function afn_get_text($val, $type = '') {
    if (!$type) {
        return '';
    }
    $data = array();
    switch ($type) {
        case 'VAR_TYPE':
            $data = array('C' => '字符', 'N' => '数字', 'T' => '文本', 'A' => '数组', 'E' => '枚举');
            break;
        case 'VAR_GROUP':
            $data = array('B' => '基本', 'C' => '内容', 'U' => '用户', 'S' => '系统');
            break;
    }
    return array_key_exists($val, $data) ? $data[$val] : '';
}
